// apis
import { getUserInfo } from '@/api/common'
// others
import { defineStore } from 'pinia'
import { ref } from 'vue'
import cache from '@/utils/cache'

export default defineStore('user', () => {
  const loginInfo = ref(null)
  const userInfo = ref(null)

  const setLoginInfo = (val) => {
    loginInfo.value = val
  }
  const setUserInfo = (val) => {
    userInfo.value = val
  }

  const getUserInfoFun = async () => {
    const res = await getUserInfo()
    if (res?.errno === 0) {
      setUserInfo(res.data)
      cache.set('userInfo', res.data)
    }
  }

  return {
    loginInfo,
    setLoginInfo,
    userInfo,
    setUserInfo,
    getUserInfoFun
  }
})
